<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\api\service\User as UserService;
use app\common\model\ding\ShopCollect as ShopCollectModel;
use cores\exception\BaseException;
use think\facade\Db;

/**
 * 客户
 * Class UserAddress
 * @package app\common\model
 */
class ShopCollect extends ShopCollectModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',

	];

	public function getList() {
		// 当前用户ID
		$userId = UserService::getCurrentLoginUserId();
		// 获取列表数据
		return $this->with(['shop.logoImage'])->where('user_id', '=', $userId)
			->order(['create_time' => 'desc'])
			->paginate(15)->each(function ($item, $key) {
			$foods = Db::name('ding_shop_foods_rel')
				->alias('rel')
				->join('ding_shop_foods f', 'f.food_id=rel.food_id')
				->field('f.name')
				->where('rel.shop_id', $item['shop_id'])
				->select()->toArray();
			if ($foods) {
				$caixi = array_column($foods, 'name');
				$item['caixi'] = implode(' | ', $caixi);
			} else {
				$item['caixi'] = '';
			}
			$item['area'] = Db::name('region')->where('id', $item['shop']['cascader'][2])->value('name');

			return $item;
		});
	}

	/**
	 * 新增
	 * @param array $data
	 * @return mixed
	 * @throws BaseException
	 */
	public function add($shopId, $userId) {

		$this->save([

			'shop_id' => $shopId,
			'user_id' => $userId,

			'store_id' => self::$storeId,
		]);

		return true;

	}

	public function del() {
		$this->delete();

		return true;
	}

	public function delAll($data) {
		$this->where('id', 'in', $data['checkedIds'])->delete();

		return true;
	}

}
